// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	async get_now_data(imei) {
		imei = imei.trim()
		if (!imei) {
			return {
				errCode: 'INVALID_NOW_DATA',
				errMsg: 'imei不可为空'
			}
		}
		const db = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		const res = await db.collection("gf-jc")
			.where("imei=='" + imei + "'")
			.limit(1)
			.orderBy("_id desc")
			.get()
		// const arr = res.data
		// const data2=res.data[1]
		let obj = {

		}
		// arr.forEach(e => {

		// 	if (e.type == 'PH') {
		// 		obj['PH'] = HexToSingle(e.value)
		// 		console.log(e.value,"+++")
		// 		const t="40E66666";
		// 		console.log(HexToSingle(t))
		// 	}
		// 	if (e.type == 'T') {
		// 		obj['T'] = HexToSingle(e.value)
		// 	}
		// 	if(e.type=='OD'){
		// 		obj['OD']=HexToSingle(e.value)
		// 	}
		// 	obj['imei'] = e.imei
		// 	obj['create_time']=e.create_time
		// });
		return {
			data: res.data
		}

	},
	async get_today_data(imei, type) {
		// console.log("++++++",type)
		let count = 60;
		if (type == 0) {
			count = 60
		}
		if (type == 1) {
			count = 1440
		}
		if (count == 2) {
			count = 10080;
		}
		imei = imei.trim()
		if (!imei) {
			return {
				errCode: 'INVALID_NOW_DATA',
				errMsg: 'imei不可为空'
			}
		}
		const db = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		const today = new Date().getTime();
		// console.log(new Date().getDate())
		// console.log("++++",today)
		const res = await db.collection("gf-jc")
			.where("imei=='" + imei + "'&&create_time<" + today)
			.limit(count)
			.orderBy("_id desc")
			.get()

		return {
			data: res.data
		}
	},
	async get_by_day(imei, start, end) {
		if (!imei) {
			return {
				errCode: 'INVALID_NOW_DATA',
				errMsg: 'imei不可为空'
			}
		}
		const db = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		console.log("=======", imei, start, end)
		// const where_str = `imei==${imei}&&create_time>=dateFromString('${start}').getTime()&&create_time<=dateFromString('${end}').getTime()`;
		const where_str = `imei=='${imei}'&&create_time>=new Date('${start}').getTime()&&create_time<=new Date('${end}').getTime()`;
		const res = await db.collection("gf-jc")
			.where(where_str)
			.groupBy('dateToString(add(new Date(0),create_time),"%Y-%m-%d","+0800") as date')
			.groupField(
				'avg(temp) as temp_a,avg(ph) as ph_a,avg(odo) as odo_a,avg(nh4) as nh4_a, max(temp) as temp_h,min(temp) as temp_l,max(ph) as ph_h,min(ph) as ph_l,max(odo) as odo_h,min(odo) as odo_l,max(nh4) as nh4_h,min(nh4) as nh4_l'
				)
				.orderBy("date desc")
			.get()

		return {
			data:res.data
		}

	},
	_timing: function(param) {
		console.log('触发时间：', param.Time)
		console.log('triggered by timing')
	}
}